package com.cyk.controller;

import com.cyk.dao.LagouAuthCodeDao;
import com.cyk.dao.LagouTokenDao;
import com.cyk.dao.LagouUserDao;
import com.cyk.feigh.CodeFeighClient;
import com.cyk.pojo.LagouToken;
import com.cyk.pojo.LagouUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    LagouTokenDao lagouTokenDao;
    @Autowired
    LagouAuthCodeDao lagouAuthCodeDao;
    @Autowired
    LagouUserDao lagouUserDao;
    @Autowired
    private CodeFeighClient codeFeighClient;

    /*
    注册接⼝，true成功，false失败
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        if (codeFeighClient.validate(email, code) == 0) {
            //已经注册
            LagouUser lagouUser = new LagouUser();
            lagouUser.setEmail(email);
            if (lagouUserDao.findOne(Example.of(lagouUser)).isPresent()) {
                return false;
            }
            lagouUser.setPassword(password);
            return lagouUserDao.save(lagouUser).getId() != 0;
        }
        return false;
    }

    /*
    是否已注册，根据邮箱判断,true代表已经注册过， false代表尚未注册
     */
    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        return lagouUserDao.findOne(Example.of(lagouUser)).isPresent();
    }

    /*
    登录接⼝，验证⽤用户名密码合法性，
    根据⽤用户名和 密码⽣生成token，
    token存⼊入数据库，并写⼊入cookie 中，
    登录成功返回邮箱地址，重定向到欢迎⻚页
     */
    @GetMapping("/login/{email}/{password}")
    public String login(HttpServletResponse response, @PathVariable String email, @PathVariable String password) {
        LagouUser lagouUser = new LagouUser();
        lagouUser.setEmail(email);
        lagouUser.setPassword(password);
        LagouUser lagouUser1 = lagouUserDao.findOne(Example.of(lagouUser)).get();
        if (lagouUser1 == null)
            return null;

        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(UUID.randomUUID().toString());
        if (lagouTokenDao.save(lagouToken).getId() != 0) {
            //写入cookie
            response.addCookie(new Cookie("token", lagouToken.getToken()));
        }
        return lagouToken.getToken();
    }

    /*
    邮箱地址 根据token查询⽤用户登录邮箱接⼝
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable String token) {
        LagouToken lagouToken = new LagouToken();
        lagouToken.setToken(token);

        LagouToken lagouToken1 = lagouTokenDao.findOne(Example.of(lagouToken)).get();
        if (lagouToken1.getId() != 0) {
            return lagouToken1.getEmail();
        }
        return "";
    }
}
